//
//  LiveViewController.m
//  BasketballClub
//
//  Created by 孙鸿吉 on 16/1/6.
//  Copyright © 2016年 孙鸿吉. All rights reserved.
//

#import "LiveViewController.h"
#import "YesterdayViewController.h"
#import "TodayViewController.h"
#import "TomorrowViewController.h"
#import "LiveView.h"
#import "LiveTableViewCell.h"
#import "SearchViewController.h"
#import <AFNetworking.h>
@interface LiveViewController ()
///根视图
@property(nonatomic,strong)LiveView *liveView;
///昨日
@property(nonatomic,strong)YesterdayViewController *yesterdayVC;
///今日
@property(nonatomic,strong)TodayViewController *todayVC;
///明日
@property(nonatomic,strong)TomorrowViewController *tomorrowVC;

///当前视图控制器
@property(nonatomic,strong)UIViewController *currentVC;
///向左轻扫的手势
@property(nonatomic,strong)UISwipeGestureRecognizer *leftSwipeGestureRecognizer;
///向右轻扫的手势
@property(nonatomic,strong)UISwipeGestureRecognizer *rightSwipeGestureRecognizer;
///无网络时候的弹框控制器
@property(nonatomic,strong)UIAlertController *alertCon;




@end

@implementation LiveViewController
//视图重新加载
-(void)loadView{
    self.liveView = [[LiveView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    
    self.view = self.liveView;
}




- (void)viewDidLoad {
    [super viewDidLoad];
    //添加自控制器
    [self addSubControllers];
#pragma mark - 给segment添加轻扫手势实现循环切换
    //向左轻扫手势的初始化
    self.leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipesAction:)];
    //向右轻扫的手势的初始化
    self.rightSwipeGestureRecognizer =[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipesAction:)];
    //指定左手势
    self.leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    //指定右手势
    self.rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    //给当前视图添加左右手势
    [self.currentVC.view addGestureRecognizer:self.leftSwipeGestureRecognizer];
    
    [self.currentVC.view addGestureRecognizer:self.rightSwipeGestureRecognizer];
    //给navigation视图的左键添加事件
    [self.liveView.rButton addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    //调用无网络判断
//    [self determineNetwork];
    
    
    
}

#pragma mark - 网络判断
//
////网络判断
//-(void)determineNetwork
//
//{
//    
//    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
//    
//    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
//        //如果当前网络状态为无网络状态
//        if (status == AFNetworkReachabilityStatusNotReachable) {
//            CGFloat version = [[[UIDevice currentDevice] systemVersion]floatValue];
//            if (version >= 8.0) {
//                //弹出以下弹框
//                self.alertCon = [UIAlertController alertControllerWithTitle:@"提示" message:@"网络异常 请检查网络" preferredStyle:UIAlertControllerStyleAlert];
//                
//                UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//                    
//                }];
//                
//                [self.alertCon addAction:action];
//                
//                [self presentViewController:self.alertCon animated:YES completion:nil];
//                
//            }else{
//                UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络异常 请检查网络" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
//                [alertView show];
//            }
//
//            }
//            
//    }];
//    
//}





#pragma mark - 手势的添加(实现循环切换)
-(void)handleSwipesAction:(UISwipeGestureRecognizer*)sender
{
    
    
    if (sender.direction == UISwipeGestureRecognizerDirectionLeft && self.currentVC == self.todayVC) {
        self.liveView.segmentControl.selectedSegmentIndex = 2;
        [self replaceController:self.currentVC newController:self.tomorrowVC];
        
        [self.tomorrowVC.view addGestureRecognizer:self.leftSwipeGestureRecognizer];
        
        [self.tomorrowVC.view addGestureRecognizer:self.rightSwipeGestureRecognizer];
        
        
    }else  if (sender.direction == UISwipeGestureRecognizerDirectionRight && self.currentVC == self.todayVC){
        self.liveView.segmentControl.selectedSegmentIndex = 0;
        [self replaceController:self.currentVC newController:self.yesterdayVC];
        [self.yesterdayVC.view addGestureRecognizer:self.leftSwipeGestureRecognizer];
        [self.yesterdayVC.view addGestureRecognizer:self.rightSwipeGestureRecognizer];
    }
    
    if (sender.direction == UISwipeGestureRecognizerDirectionLeft && self.currentVC == self.yesterdayVC) {
        self.liveView.segmentControl.selectedSegmentIndex = 1;
        [self replaceController:self.currentVC newController:self.todayVC];
        [self.todayVC.view addGestureRecognizer:self.leftSwipeGestureRecognizer];
        [self.todayVC.view addGestureRecognizer:self.rightSwipeGestureRecognizer];
        
        
    }else  if (sender.direction == UISwipeGestureRecognizerDirectionRight && self.currentVC == self.yesterdayVC){
        self.liveView.segmentControl.selectedSegmentIndex = 2;
        [self replaceController:self.currentVC newController:self.tomorrowVC];
        [self.tomorrowVC.view addGestureRecognizer:self.leftSwipeGestureRecognizer];
        [self.tomorrowVC.view addGestureRecognizer:self.rightSwipeGestureRecognizer];
        
    }
    
    
    if (sender.direction == UISwipeGestureRecognizerDirectionLeft && self.currentVC == self.tomorrowVC) {
        self.liveView.segmentControl.selectedSegmentIndex = 0;
        [self replaceController:self.currentVC newController:self.yesterdayVC];
        [self.yesterdayVC.view addGestureRecognizer:self.leftSwipeGestureRecognizer];
        [self.yesterdayVC.view addGestureRecognizer:self.rightSwipeGestureRecognizer];
        
        
    }else  if (sender.direction == UISwipeGestureRecognizerDirectionRight && self.currentVC == self.tomorrowVC){
        self.liveView.segmentControl.selectedSegmentIndex = 1;
        [self replaceController:self.currentVC newController:self.todayVC];
        [self.todayVC.view addGestureRecognizer:self.leftSwipeGestureRecognizer];
        [self.todayVC.view addGestureRecognizer:self.rightSwipeGestureRecognizer];
        
    }
    
}
#pragma mark  - Button的点击事件

-(void)leftButtonAction:(UIButton*)sender
{
    SearchViewController *searchVC  = [[SearchViewController alloc]init];
    UINavigationController *rootNC = [[UINavigationController alloc]initWithRootViewController:searchVC];
    [self presentViewController:rootNC animated:YES completion:^{
        
    }];
    
}



#pragma mark - 添加子视图控制器
-(void)addSubControllers{
    //设置Frame
    [self.todayVC.view setFrame:CGRectMake(0, 104*LAYOUT_SIZE_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT+104*LAYOUT_SIZE_HEIGHT)];
    [self addChildViewController:self.todayVC];
    //设置Frame
    [self.yesterdayVC.view setFrame:CGRectMake(0, 104*LAYOUT_SIZE_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT+104*LAYOUT_SIZE_HEIGHT)];
    [self addChildViewController:self.yesterdayVC];
    //设置Frame
    [self.tomorrowVC.view setFrame:CGRectMake(0, 104*LAYOUT_SIZE_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT+104*LAYOUT_SIZE_HEIGHT)];
    [self addChildViewController:self.tomorrowVC];
    
    
    //设置当前显示的那个控制器视图
    [self.liveView addSubview:self.todayVC.view];
    self.currentVC = self.todayVC;
    self.currentVC.view.hidden = NO;
    //给segment添加事件
    [self.liveView.segmentControl addTarget:self action:@selector(segmentedControlAction:) forControlEvents:UIControlEventValueChanged];
}



#pragma mark - segmentedControl事件
- (void)segmentedControlAction:(UISegmentedControl *)sender
{
    //判断当前是哪个控制器
    if (self.currentVC == self.yesterdayVC && sender.selectedSegmentIndex == 0) {
        
        return;
        
    }
    if (self.currentVC == self.tomorrowVC && sender.selectedSegmentIndex == 2) {
       
        return;
        
    }
    if (self.currentVC == self.todayVC && sender.selectedSegmentIndex == 1) {
        return;
     
    }else{
        NSInteger index = sender.selectedSegmentIndex;
        
        switch (index) {
            case 1:{
                [self replaceController:self.currentVC newController:self.todayVC];
               
                break;
            }
            case 0:{
                
                [self replaceController:self.currentVC newController:self.yesterdayVC];
               
                break;
            }
            case 2:{
                [self replaceController:self.currentVC newController:self.tomorrowVC];
                
                break;
            }
                
            default:{
                break;
            }
        }
        
        
    }
}

#pragma mark - 切换segmentedControl标签方法
- (void)replaceController:(UIViewController *)oldController newController:(UIViewController *)newController
{
    //添加为子控制器
    [self addChildViewController:newController];
    //当一个新的控制器出现并替代之前的控制器时执行以下方法
    [self transitionFromViewController:oldController toViewController:newController duration:0.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) {
        if (finished) {
            //原来的控制器隐藏
            oldController.view.hidden  =YES;
            [oldController willMoveToParentViewController:nil];
            //新的控制器显示
            newController.view.hidden = NO;
            self.currentVC = newController;
            //给当前视图添加左右手势
            [self.currentVC.view addGestureRecognizer:self.leftSwipeGestureRecognizer];
            
            [self.currentVC.view addGestureRecognizer:self.rightSwipeGestureRecognizer];

        }else{
            self.currentVC = oldController;
            //给当前视图添加左右手势
            [self.currentVC.view addGestureRecognizer:self.leftSwipeGestureRecognizer];
            
            [self.currentVC.view addGestureRecognizer:self.rightSwipeGestureRecognizer];

        }
    }];
    
    
    
    
    
}

#pragma mark - 懒加载
-(YesterdayViewController*)yesterdayVC
{
    if (_yesterdayVC == nil) {
        _yesterdayVC = [[YesterdayViewController alloc
                         ]init];
    }
    return _yesterdayVC;
}


-(TodayViewController*)todayVC
{
    if (_todayVC == nil) {
        _todayVC = [[TodayViewController alloc]init];
    }
    return _todayVC;
}

-(TomorrowViewController*)tomorrowVC
{
    if (_tomorrowVC == nil) {
        _tomorrowVC = [[TomorrowViewController alloc]init];
        
    }
    return _tomorrowVC;
}








@end
